<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet 
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2011 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------

class GroupLogic extends  BaseService
{
	
	public static function addGroup($data){
		return self::add('group', $data);
	}
	
	
	public static function getGroups($uid,$limit=20)
	{		
		$where = empty($uid) ? "status=0 ORDER BY id desc" : "user_id=$uid and status=0 ORDER BY id desc";
		$r = self::getsByPage('group', $where,0,$limit);
		return $r;
	}
	
	public static function getMyJoinGroups($userid,$limit)
	{
		//load from list 
		
	}
	
	/**
	 * return recommand groups 
	 * Enter description here ...
	 * @param unknown_type $userid
	 */
	public static function getRecommandGroups($userid='')
	{
		if($userid)
		{
			$recommandGroups = '';
		}
		//@TODO: neend to change the recommand groups 
		$where = 'status =0 ORDER BY ID DESC LIMIT 8';
		$recommandGroups = self::gets('group', $where);	
		return $recommandGroups;
	}
	
	public static function getActiveGroups($userid='')
	{
		if($userid)
		{
			$recommandGroups = '';
		}
		//@TODO: neend to change the recommand groups 
		$where = 'status >0 ORDER BY ID DESC LIMIT 8';
		$recommandGroups = self::gets('group', $where);	
		return $recommandGroups;
	}
	
	/**
	 * get active user keys from redis
	 * @TODO user redis cache
	 * @param unknown_type $groupid
	 */
	public static function getActiveUser($groupid='')
	{
		$users = false;
		//@TODO: read from cache ,if a user post a topic and the comment from the page then add to the cache
		$key = empty($groupid) ? 'group:activeusers' : "group:$groupid:activeusers";
		$userslist = self::redisGetList($key, 0, 10);
		//@TODO need to be change.....
		$userslist = !empty($userslist) ? $userslist : array('13166685156566520','13165031225585452','13161645655358470');
		$users = AccountService::accountin($userslist);
		return $users;
	}
	
	/**
	 * group/index return data func
	 * Enter description here ...
	 * @param unknown_type $array
	 */
	public static function webPageIndex($uid)
	{
		
		//load group and load topic and load other
		$data = false;
		logTrace(__CLASS__.'/'.__FUNCTION__.':uid  '.$uid);
		$limit = 20;
		//get the recommand
		$data['recommand'] = self::getRecommandGroups($uid);
		//var_dump($data['recommand']);
		//active groups
		$data['active'] = self::getActiveGroups($uid);
		//my join groups
		$data['group'] = self::getGroups($uid,$limit);
		//my join group's topics
		$topics = TopicLogic::getsMyJoinGroupTopics($data['group']);
		$data['topics'] = array();
		if(is_array($topics))
		{
			$data['topics'] = self::mergeGroupInfoToArray(mergeUserinfoToArray($topics));
		}
		
		//my join group 
		$data['mygroup'] = self::getMyJoinGroups($uid, $limit);
		
		//get active users
		$data['activeusers'] = self::getActiveUser('');
		//@TODO add other group about data do display 
		
		return $data;
	}
	
	public static function webPageGroup($params)
	{
		if(!empty($params['enname']))
		{
			//@TODO :use cache????
			$id = self::queryField('group',"enname = '{$params['enname']}'",'id');
			return $id>0 ? self::redisGetHashes(array($id)) : array();
		}
	}
	
	
	
	
	public static function mergeGroupInfoToArray($targetArray)
	{
		$relKey = 'group_id';
		$group = '';
		$where = "status=0 and id in ".strin($targetArray,$relKey);
		$groupData = self::getsKeyID('group', $where,'id,title,enname');
		//var_dump($groupData);
		//logTrace(__CLASS__.'/'.__FUNCTION__.':GROUP INFO  '.implode(',', $groupData));
		if(is_array($targetArray)){
			  foreach($targetArray as $key =>&$Value)
			  {
			    if(array_key_exists($relKey, $Value) && is_array($Value))
			    {
			    	$group = $Value[$relKey];
			    	if(isset($groupData[$group])){
			      		$Value['grouptitle'] = $groupData[$group]['title'];
			      		$Value['groupenname'] = $groupData[$group]['enname'];
			    	}else{
			    		$Value['grouptitle'] = '';
			    		$Value['groupenname'] = '';
			    	}
			    }
			  }
		}
		return $targetArray;
	}
	
	
}
